package utils

import (
	"fmt"
	"k8s.io/client-go/kubernetes/scheme"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
)

func get_Node() {
	// 1. 加载kubeconfig文件
	kubeconfig := "/path/to/kubeconfig" // 替换为实际的kubeconfig路径
	config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
	if err != nil {
		panic(err.Error())
	}

	// 2. 创建RESTClient
	config.APIPath = "api"
	config.GroupVersion = &scheme.Scheme.PrioritizedVersionsForGroup("")[0]
	config.NegotiatedSerializer = scheme.Codecs
	// 3. 加载restClient
	restClient, err := rest.RESTClientFor(config)
	if err != nil {
		panic(err.Error())
	}

	// 4. 发送GET请求获取Node列表
	result := &struct {
		Items []map[string]interface{} `json:"items"`
	}{}
	err = restClient.Get().
		Namespace("default").
		Resource("nodes").
		//VersionedParams(&struct {
		Do().Into(result)
	if err != nil {
		panic(err.Error())
	}

	// 4. 打印结果
	fmt.Printf("Nodes: %+v\n", result.Items)
}
